package ToYesterday.Day09_stack;

import java.util.Stack;

public class Solution {
//Hashmap

    public static void main(String[] args) {
        String str="(){}}{";
        System.out.println(isvalid(str));
    }

    public static boolean isvalid(String s){
        char[] strs = s.toCharArray();
        Stack<Character> stack = new Stack<Character>();
        for (char str: strs){
            if (str== '{' || str == '(' || str == '[')stack.add(str);
            else if (!stack.isEmpty()){
                switch (str){
                    case '}':
                        if (stack.peek()!='{')return false;
                        break;
                    case ')':
                        if (stack.peek()!='(')return false;
                        break;
                    case ']':
                        if (stack.peek()!='[')return false;
                        break;
                }
                stack.pop();
            }else return false;
        }

        return stack.isEmpty();
    }


    public static boolean isValid(String s) {
        char[] str = s.toCharArray();
        Node dummpy = new Node();
        for (int i = 0; i < str.length; i++) {
            if (str[i] == '{' || str[i] == '(' || str[i] == '[') {
                Node nNode = new Node(str[i]);
                nNode.next = dummpy.next;
                dummpy.next = nNode;
            } else if (dummpy.next!=null) {
                switch (str[i]) {
                    case '}':
                        if (dummpy.next.val == '{') {
                            dummpy.next = dummpy.next.next;
                        } else return false;
                        break;
                    case ')':
                        if (dummpy.next.val == '('){
                            dummpy.next = dummpy.next.next;
                        }else  return false;
                        break;
                    case ']':
                        if (dummpy.next.val == '['){
                            dummpy.next = dummpy.next.next;
                        }else return false;
                }
            }else return false;
        }
        if (dummpy.next==null){
            return true;
        }else return false;

    }
}
